import {take, call, put, fork, cancel} from 'redux-saga/effects';
import AuthManager from '../services/auth'
import * as Actions from '../actions/index'
import * as Types from '../actions/ActionTypes.js'
import * as api from '../services/enterprise';
import {message} from 'antd';
import callServiceApi from './utils';

function* watchCompanysQuery() {
  while (true) {
    const { payload } = yield take(Types.COMPANYS_QUERY.REQUEST);
    yield fork(companysQuery, payload);
  }
}
function* watchCompanysDelete() {
  while (true) {
    const { payload } = yield take(Types.COMPANYS_DELETE.REQUEST);
    yield fork(companysDelete, payload);
    const action = yield take([Types.COMPANYS_DELETE.SUCCESS]);
    if(action.type == Types.COMPANYS_DELETE.SUCCESS) {
      message.info("操作成功！");
      yield put(Actions.companysDeleteNotify()(payload));
    }
  }
}

function* watchEnterpiseCreate() {
  while (true) {
    const { payload } = yield take(Types.ENTERPRISE_CREATE.REQUEST);
    yield fork(enterpiseCreate, payload);
    const action = yield take([Types.ENTERPRISE_CREATE.SUCCESS]);
    if(action.type == Types.ENTERPRISE_CREATE.SUCCESS) {
      message.info("录入成功！");
    }
  }
}

function* watchCompanyNotify() {
  while (true) {
    const { payload } = yield take(Types.COMPANY_NOTIFY.REQUEST);
    yield fork(companyNotify, payload);
    const action = yield take([Types.COMPANY_NOTIFY.SUCCESS]);
    if(action.type == Types.COMPANY_NOTIFY.SUCCESS) {
      message.info("操作成功！");
    }
  }
}

function* companysQuery(param) {
  yield fork(callServiceApi, Actions.companysQuery, api.companysQuery, param);
}

function* companysDelete(param) {
  yield fork(callServiceApi, Actions.companysDelete, api.companysDelete, param);
}

function* companyNotify(param) {
  yield fork(callServiceApi, Actions.companyNotify, api.companyNotify, param);
}

function* enterpiseCreate(param) {
  yield fork(callServiceApi, Actions.enterpiseCreate, api.enterpiseCreate, param);
}

export default function*() {
  yield [
    fork(watchCompanysQuery),
    fork(watchCompanysDelete),
    fork(watchCompanyNotify),
    fork(watchEnterpiseCreate)
  ];
}
